{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import json\n",
    "from keras.models import Model\n",
    "from keras.layers import Input\n",
    "from keras.layers.convolutional import Conv2D\n",
    "from keras.layers.pooling import MaxPooling2D, AveragePooling2D\n",
    "from keras.layers.normalization import BatchNormalization\n",
    "from keras import backend as K\n",
    "from collections import OrderedDict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def format_decimal(arr, places=6):\n",
    "    return [round(x * 10**places) / 10**places for x in arr]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "DATA = OrderedDict()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### pipeline 12"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "random_seed = 1012\n",
    "data_in_shape = (8, 8, 2)\n",
    "\n",
    "layers = [\n",
    "    Conv2D(4, (3,3), activation='relu', padding='same', strides=(1,1), data_format='channels_last', use_bias=True),\n",
    "    Conv2D(4, (3,3), activation='relu', padding='valid', strides=(1,1), data_format='channels_last', use_bias=True),\n",
    "    AveragePooling2D(pool_size=(2,2), strides=(1,1), padding='same', data_format='channels_last')\n",
    "]\n",
    "\n",
    "input_layer = Input(shape=data_in_shape)\n",
    "x = layers[0](input_layer)\n",
    "for layer in layers[1:-1]:\n",
    "    x = layer(x)\n",
    "output_layer = layers[-1](x)\n",
    "model = Model(inputs=input_layer, outputs=output_layer)\n",
    "\n",
    "np.random.seed(random_seed)\n",
    "data_in = 2 * np.random.random(data_in_shape) - 1\n",
    "\n",
    "# set weights to random (use seed for reproducibility)\n",
    "weights = []\n",
    "for i, w in enumerate(model.get_weights()):\n",
    "    np.random.seed(random_seed + i)\n",
    "    weights.append(2 * np.random.random(w.shape) - 1)\n",
    "model.set_weights(weights)\n",
    "\n",
    "result = model.predict(np.array([data_in]))\n",
    "data_out_shape = result[0].shape\n",
    "data_in_formatted = format_decimal(data_in.ravel().tolist())\n",
    "data_out_formatted = format_decimal(result[0].ravel().tolist())\n",
    "\n",
    "DATA['pipeline_12'] = {\n",
    "    'input': {'data': data_in_formatted, 'shape': data_in_shape},\n",
    "    'weights': [{'data': format_decimal(w.ravel().tolist()), 'shape': w.shape} for w in weights],\n",
    "    'expected': {'data': data_out_formatted, 'shape': data_out_shape}\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### export for Keras.js tests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "filename = '../../test/data/pipeline/12.json'\n",
    "if not os.path.exists(os.path.dirname(filename)):\n",
    "    os.makedirs(os.path.dirname(filename))\n",
    "with open(filename, 'w') as f:\n",
    "    json.dump(DATA, f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"pipeline_12\": {\"input\": {\"data\": [-0.946541, 0.585593, -0.49527, 0.594532, -0.38557, 0.124664, 0.290608, 0.016082, -0.201742, -0.480332, 0.087069, -0.208842, 0.595154, -0.229272, 0.302077, -0.550916, -0.026675, -0.657993, -0.19277, 0.304198, 0.735077, -0.450206, 0.675587, -0.829729, -0.701274, 0.00237, 0.752567, 0.533195, 0.577175, 0.936814, 0.482555, 0.999201, 0.717585, -0.349124, 0.045127, 0.658608, -0.729666, -0.003321, 0.798143, -0.181835, -0.892073, -0.590138, -0.493802, 0.007057, -0.778, 0.88804, 0.690821, 0.368509, -0.9201, -0.812757, -0.04144, -0.299581, -0.705749, 0.509258, -0.74382, 0.112347, -0.731445, 0.37439, 0.70878, -0.937578, -0.689795, -0.143301, -0.020724, -0.08702, -0.834716, 0.315619, -0.822687, 0.176608, -0.70899, 0.563674, 0.289775, -0.05136, -0.280327, -0.370151, 0.755964, 0.611803, -0.838633, 0.389214, -0.697565, 0.424129, -0.599661, 0.810604, -0.317828, 0.172621, 0.03693, -0.560866, -0.686396, -0.531939, -0.556952, 0.200428, -0.512259, -0.332615, 0.264967, 0.686541, 0.20202, -0.379064, -0.148747, -0.515336, 0.569717, -0.286251, 0.904479, -0.195567, -0.476764, 0.147412, -0.535398, -0.33688, 0.061352, -0.8256, -0.465577, 0.050022, 0.928583, -0.709106, 0.644448, 0.110848, -0.395186, -0.110745, 0.323689, -0.120854, 0.937639, 0.937964, 0.183655, 0.466538, -0.019195, 0.472077, 0.951021, 0.141359, 0.552264, -0.711024], \"shape\": [8, 8, 2]}, \"weights\": [{\"data\": [-0.946541, 0.585593, -0.49527, 0.594532, -0.38557, 0.124664, 0.290608, 0.016082, -0.201742, -0.480332, 0.087069, -0.208842, 0.595154, -0.229272, 0.302077, -0.550916, -0.026675, -0.657993, -0.19277, 0.304198, 0.735077, -0.450206, 0.675587, -0.829729, -0.701274, 0.00237, 0.752567, 0.533195, 0.577175, 0.936814, 0.482555, 0.999201, 0.717585, -0.349124, 0.045127, 0.658608, -0.729666, -0.003321, 0.798143, -0.181835, -0.892073, -0.590138, -0.493802, 0.007057, -0.778, 0.88804, 0.690821, 0.368509, -0.9201, -0.812757, -0.04144, -0.299581, -0.705749, 0.509258, -0.74382, 0.112347, -0.731445, 0.37439, 0.70878, -0.937578, -0.689795, -0.143301, -0.020724, -0.08702, -0.834716, 0.315619, -0.822687, 0.176608, -0.70899, 0.563674, 0.289775, -0.05136], \"shape\": [3, 3, 2, 4]}, {\"data\": [0.114077, -0.889658, -0.472025, 0.718808], \"shape\": [4]}, {\"data\": [-0.536401, 0.404425, -0.338344, -0.818131, 0.441566, 0.867769, 0.505219, 0.689963, 0.598858, 0.268687, -0.728914, 0.878801, -0.318668, -0.305686, -0.80848, 0.195743, 0.821773, 0.716334, -0.41629, 0.169864, -0.799399, -0.27582, 0.312052, -0.021639, 0.822725, -0.417215, 0.246061, -0.188668, -0.632463, 0.781629, -0.462136, -0.881432, -0.024391, 0.94485, -0.300768, 0.615408, 0.12297, 0.540219, 0.335681, -0.355003, 0.194186, 0.922101, -0.592691, -0.345502, 0.812393, 0.922283, 0.520438, 0.878666, -0.380913, 0.41579, -0.541346, -0.72511, -0.276467, 0.539221, 0.09016, 0.344573, 0.564725, 0.392841, -0.250152, -0.200319, 0.615438, 0.658222, -0.706144, 0.066833, -0.346988, 0.490674, 0.566333, -0.210763, -0.292341, 0.583961, 0.007116, -0.958503, -0.134622, 0.7337, -0.758515, 0.97331, 0.925282, 0.962484, 0.770363, -0.255384, -0.696331, -0.163798, 0.167749, 0.65149, -0.058641, 0.289875, -0.597191, 0.532389, -0.299169, -0.373696, 0.497903, -0.874778, 0.647648, 0.61738, -0.500244, 0.308808, -0.573185, -0.078578, -0.112652, -0.970421, -0.087663, 0.69568, -0.961853, 0.980975, -0.502315, -0.187049, 0.726371, 0.604667, -0.283893, -0.422462, 0.470612, -0.069763, 0.065728, 0.948825, 0.239745, -0.085512, -0.816428, 0.951825, 0.619208, 0.343894, 0.686469, 0.083229, 0.845692, 0.068946, -0.483638, 0.20846, 0.379883, 0.30094, 0.766349, -0.363107, 0.715632, 0.893574, -0.326701, 0.044492, 0.825531, -0.860217, -0.646327, 0.304335, -0.815227, -0.319544, 0.414308, -0.549111, -0.555289, -0.567206], \"shape\": [3, 3, 4, 4]}, {\"data\": [0.255023, -0.721247, 0.235337, 0.2967], \"shape\": [4]}], \"expected\": {\"data\": [1.966993, 6.407506, 1.028256, 0.833111, 2.461879, 6.955625, 1.219838, 0.960649, 2.139994, 7.037232, 0.191582, 1.095619, 2.14032, 7.881288, 0.463705, 1.130602, 3.481145, 7.939707, 0.780836, 0.501793, 3.120827, 6.115252, 0.63426, 0.0, 2.802898, 9.563566, 0.014415, 1.58629, 2.804509, 9.773899, 0.0, 1.187023, 1.994748, 8.413921, 0.0, 0.786536, 1.112725, 7.696995, 0.382852, 1.029967, 0.893136, 7.810233, 0.382852, 0.407376, 0.0, 7.292133, 0.0, 0.012439, 1.576993, 7.708013, 0.691242, 1.909831, 1.31598, 7.716723, 0.658776, 0.97949, 1.277682, 8.590884, 0.412159, 0.14778, 2.294367, 8.122358, 0.0, 0.095695, 1.844788, 6.838662, 0.0, 0.006219, 0.0, 7.486452, 0.0, 0.012439, 0.385584, 6.769026, 0.676827, 1.472253, 0.596415, 7.417989, 0.658776, 0.784934, 1.045994, 7.755109, 0.412159, 0.490255, 2.294367, 8.622545, 0.0, 0.095695, 1.844788, 5.930583, 0.0, 0.0, 0.0, 4.311473, 0.0, 0.0, 1.585237, 5.38713, 0.0, 1.183372, 1.428917, 6.192756, 0.0, 0.732849, 0.632305, 5.193853, 0.171203, 0.884104, 0.394381, 4.12167, 0.198091, 0.626657, 0.358491, 3.254751, 0.026888, 0.085028, 0.0, 3.10686, 0.0, 0.0, 3.170474, 4.161235, 0.0, 0.935603, 1.665004, 3.325595, 0.0, 0.0, 0.071779, 3.858306, 0.342405, 1.083259, 0.788761, 2.010638, 0.396182, 1.253315, 0.716982, 2.486049, 0.053776, 0.170057, 0.0, 4.972098, 0.0, 0.0], \"shape\": [6, 6, 4]}}}\n"
     ]
    }
   ],
   "source": [
    "print(json.dumps(DATA))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
